﻿package core.ui.util
{
    import core.event.*;
    import core.ui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class GradientTuyitu extends Sprite
    {
        var ed:Tuyitu;
        var ph:Sprite;
        var dh:Sprite;
        var mtx:Matrix;
        public var colors:Array;
        public var alphas:Array;
        public var ratios:Array;
        public var preview:Sprite;
        private var color:ColorPad;
        private var opacity:Dropslider;
        private var colordrag:ColorDrag;
        var drags:Array;

        public function GradientTuyitu(param1:Tuyitu)
        {
            this.colors = new Array(0, 16777215);
            this.alphas = new Array(1, 1);
            this.ratios = new Array(0, 255);
            this.ed = param1;
            this.mtx = new Matrix();
            this.mtx.createGradientBox(255, 30, 0, 0, 0);
            this.dh = new Sprite();
            this.dh.addEventListener(MouseEvent.MOUSE_DOWN, this.AddDragMouseDown);
            this.dh.graphics.beginFill(0, 0);
            this.dh.graphics.drawRect(0, 0, 260, 10);
            this.dh.x = 8;
            this.dh.y = 50;
            addChild(this.dh);
            this.ph = new Sprite();
            this.ph.graphics.beginFill(9474192);
            this.ph.graphics.drawRect(0, 0, 260, 34);
            this.ph.graphics.beginFill(16777215);
            this.ph.graphics.drawRect(1, 1, 258, 32);
            this.ph.graphics.beginBitmapFill(new Transparent());
            this.ph.graphics.drawRect(2, 2, 256, 30);
            this.ph.graphics.endFill();
            this.ph.x = 10;
            this.ph.y = 15;
            addChild(this.ph);
            this.preview = new Sprite();
            this.preview.x = 2;
            this.preview.y = 2;
            this.ph.addChild(this.preview);
            var _loc_2:* = new Literal("rm_opacity", true);
            _loc_2.x = 0;
            _loc_2.y = 80;
            _loc_2.width = 80;
            addChild(_loc_2);
            this.opacity = new Dropslider(50, 0, 100, 100);
            this.opacity.addEventListener(SliderEvent.DRAG, this.OpacityChange, false, 0, true);
            this.opacity.addEventListener(SliderEvent.CHANGE, this.Settled, false, 0, true);
            this.opacity.x = 80;
            this.opacity.y = 80;
            addChild(this.opacity);
            var _loc_3:* = new Literal("rm_color", true);
            _loc_3.x = 115;
            _loc_3.y = 80;
            _loc_3.width = 80;
            addChild(_loc_3);
            this.color = new ColorPad(0, true);
            this.color.addEventListener(MouseEvent.MOUSE_DOWN, this.ColorMouseDown, false, 0, true);
            this.color.addEventListener(ColorPadEvent.CHOOSE_COLOR, this.ColorChange, false, 0, true);
            this.color.x = 195;
            this.color.y = 80;
            addChild(this.color);
            var _loc_4:* = new Sprite();
            _loc_4.graphics.beginFill(9474192);
            _loc_4.graphics.drawRect(0, 0, 20, 19);
            _loc_4.graphics.beginFill(14146528);
            _loc_4.graphics.drawRect(1, 1, 18, 17);
            _loc_4.x = 240;
            _loc_4.y = 80;
            addChild(_loc_4);
            var _loc_5:* = new IconButton(param1, "rm_delete", new TrashIcon());
            _loc_5.addEventListener(MouseEvent.CLICK, this.DeleteDragClick, false, 0, true);
            _loc_5.x = 4;
            _loc_5.y = 4;
            _loc_4.addChild(_loc_5);
            this.SetGradient();
            return;
        }// end function

        public function SetGradient(TrashIcon:int = 0) : void
        {
            var _loc_2:ColorDrag = null;
            var _loc_3:int = 0;
            this.drags = new Array();
            this.colordrag = null;
            while (this.dh.numChildren > 0)
            {
                
                this.dh.removeChildAt(0);
            }
            while (_loc_3 < this.ratios.length)
            {
                
                _loc_2 = new ColorDrag(this.colors[_loc_3], this.alphas[_loc_3], _loc_3);
                _loc_2.addEventListener(MouseEvent.MOUSE_DOWN, this.DragMouseDown, false, 1, true);
                this.drags.push(_loc_2);
                _loc_2.x = this.ratios[_loc_3];
                this.dh.addChild(_loc_2);
                if (_loc_3 == TrashIcon)
                {
                    this.colordrag = _loc_2;
                    this.colordrag.Mark(true);
                    this.color.Set(this.colors[_loc_3]);
                    this.opacity.Set(this.alphas[0] * 100);
                }
                _loc_3++;
            }
            this.RedrawPreviewGradient();
            return;
        }// end function

        private function RedrawPreviewGradient() : void
        {
            this.preview.graphics.clear();
            this.preview.graphics.beginGradientFill("linear", this.colors, this.alphas, this.ratios, this.mtx);
            this.preview.graphics.drawRect(0, 0, 256, 30);
            this.graphics.endFill();
            return;
        }// end function

        private function Settled(... args) : void
        {
            dispatchEvent(new ChangeEvent("redraw"));
            return;
        }// end function

        private function AddDragMouseDown(event:MouseEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_2:* = this.dh.mouseX - 4;
            while (this.ratios[_loc_3] < _loc_2 && _loc_3++ <= this.ratios.length)
            {
                
            }
            this.ratios.splice(_loc_3, 0, _loc_2);
            this.colors.splice(_loc_3, 0, 10066329);
            this.alphas.splice(_loc_3, 0, 1);
            this.SetGradient(_loc_3);
            return;
        }// end function

        private function DeleteDragClick(event:MouseEvent) : void
        {
            if (this.ratios.length > 2)
            {
                this.ratios.splice(this.colordrag.nr, 1);
                this.colors.splice(this.colordrag.nr, 1);
                this.alphas.splice(this.colordrag.nr, 1);
                this.SetGradient();
            }
            return;
        }// end function

        private function DragMouseDown(event:MouseEvent) : void
        {
            event.stopPropagation();
            this.colordrag.Mark(false);
            this.colordrag = event.target as ColorDrag;
            this.colordrag.Mark(true);
            this.colordrag.startDrag(false, new Rectangle(0, 0, 255, 0));
            this.color.Set(this.colordrag.color);
            this.opacity.Set(this.colordrag.opacity * 100);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.DragMouseMove, false, 1, true);
            stage.addEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp, false, 1, true);
            return;
        }// end function

        private function DragMouseMove(event:MouseEvent) : void
        {
            var _loc_2:int = 0;
            var _loc_3:Number = NaN;
            var _loc_4:uint = 0;
            var _loc_5:Number = NaN;
            var _loc_6:ColorDrag = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            this.ratios[this.colordrag.nr] = this.colordrag.x;
            while (_loc_8 < this.ratios.length)
            {
                
                _loc_2 = _loc_8;
                while (_loc_2 < this.ratios.length)
                {
                    
                    if (this.ratios[_loc_8] > this.ratios[_loc_2])
                    {
                        _loc_3 = this.ratios[_loc_2];
                        _loc_4 = this.colors[_loc_2];
                        _loc_5 = this.alphas[_loc_2];
                        this.ratios.splice(_loc_2, 1);
                        this.colors.splice(_loc_2, 1);
                        this.alphas.splice(_loc_2, 1);
                        this.ratios.splice(_loc_8, 0, _loc_3);
                        this.colors.splice(_loc_8, 0, _loc_4);
                        this.alphas.splice(_loc_8, 0, _loc_5);
                        if (this.colordrag.nr == _loc_2)
                        {
                            _loc_2 = _loc_8;
                            _loc_8 = this.colordrag.nr;
                        }
                        _loc_7 = 0;
                        while (_loc_7 < this.dh.numChildren)
                        {
                            
                            _loc_6 = this.dh.getChildAt(_loc_7) as ColorDrag;
                            if (_loc_6.nr == _loc_2)
                            {
                                _loc_6.nr = _loc_8;
                                break;
                            }
                            _loc_7++;
                        }
                        this.colordrag.nr = _loc_2;
                    }
                    _loc_2++;
                }
                _loc_8++;
            }
            this.RedrawPreviewGradient();
            return;
        }// end function

        private function DragMouseUp(event:MouseEvent) : void
        {
            this.colordrag.stopDrag();
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.DragMouseMove, false);
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp, false);
            this.Settled();
            return;
        }// end function

        public function ColorMouseDown(event:MouseEvent) : void
        {
            new ColorPicker(this.ed, event.target as ColorPad);
            return;
        }// end function

        private function ColorChange(event:ColorPadEvent) : void
        {
            this.colordrag.SetColor(event.color);
            this.colors[this.colordrag.nr] = event.color;
            this.RedrawPreviewGradient();
            this.Settled();
            return;
        }// end function

        private function OpacityChange(event:SliderEvent) : void
        {
            this.colordrag.SetOpacity(this.opacity.value / 100);
            this.alphas[this.colordrag.nr] = this.opacity.value / 100;
            this.RedrawPreviewGradient();
            return;
        }// end function

        private function DeleteDragMouseOver(event:MouseEvent) : void
        {
            this.ed.hover.Set("rm_remove_color_point");
            return;
        }// end function

    }
}
